package com.guli.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;
import com.guli.common.utils.R;
import com.guli.gulimall.product.dao.AttrDao;
import com.guli.gulimall.product.dao.SkuInfoDao;
import com.guli.gulimall.product.dao.SpuInfoDao;
import com.guli.gulimall.product.dao.SpuInfoDescDao;
import com.guli.gulimall.product.entity.*;
import com.guli.gulimall.product.feign.CouponFeignService;
import com.guli.gulimall.product.service.*;
import com.guli.gulimall.product.to.MemberPriceTo;
import com.guli.gulimall.product.to.SkuFullReductionTo;
import com.guli.gulimall.product.to.SkuLadderTo;
import com.guli.gulimall.product.to.SpuBoundsTo;
import com.guli.gulimall.product.vo.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    private CouponFeignService couponFeignService;
    @Autowired
    private SpuInfoDescDao spuInfoDescDao;
    @Autowired
    private SpuImagesService imagesService;
    @Autowired
    private AttrDao attrDao;
    @Autowired
    private SkuInfoDao skuInfoDao;
    @Autowired
    private ProductAttrValueService productAttrValueService;
    @Autowired
    private SkuImagesService skuImagesService;
    @Autowired
    private SkuSaleAttrValueService skuSaleAttrValueService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R addSpu(SpuVo spuVo) {
        //gulimall_pms
        //1、pms_spu_info 商品的基本信息表
        SpuInfoEntity spuInfoEntity = new SpuInfoEntity();
        BeanUtils.copyProperties(spuVo,spuInfoEntity);
        spuInfoEntity.setCreateTime(new Date());
        spuInfoEntity.setUpdateTime(new Date());
        baseMapper.insert(spuInfoEntity);
        //2、pms_spu_info_desc 商品介绍信息（介绍大图）
        SpuInfoDescEntity spuInfoDescEntity = new SpuInfoDescEntity();
        spuInfoDescEntity.setSpuId(spuInfoEntity.getId());
        List<String> decript = spuVo.getDecript();
        String url="";
        for (String s : decript) {
            url+=","+s;
        }
        spuInfoDescEntity.setDecript(url);
        spuInfoDescDao.insert(spuInfoDescEntity);
        //pms_spu_images (所有sku的小图信息)
        List<String> images = spuVo.getImages();
        if (!images.isEmpty()) {
            List<SpuImagesEntity> collect = images.stream().map(img -> {
                SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
                spuImagesEntity.setSpuId(spuInfoEntity.getId());
                spuImagesEntity.setImgUrl(img);
                spuImagesEntity.setImgSort(0);
                return spuImagesEntity;
            }).collect(Collectors.toList());
            imagesService.saveBatch(collect);
        }
        //pms_product_attr_value 所有spu的规格参数
        List<BaseAttrs> baseAttrs = spuVo.getBaseAttrs();
        if (!baseAttrs.isEmpty()) {
            List<ProductAttrValueEntity> collect = baseAttrs.stream().map(attr -> {
                ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
                productAttrValueEntity.setSpuId(spuInfoEntity.getId());
                productAttrValueEntity.setAttrId(attr.getAttrId());
                productAttrValueEntity.setAttrValue(attr.getAttrValues());
                productAttrValueEntity.setQuickShow(attr.getShowDesc());
                AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                productAttrValueEntity.setSearchType(attrEntity.getSearchType());
                productAttrValueEntity.setAttrName(attrEntity.getAttrName());
                return productAttrValueEntity;
            }).collect(Collectors.toList());
            productAttrValueService.saveBatch(collect);
        }
        //sms_spu_bounds spu积分表
        Bounds bounds = spuVo.getBounds();
        if (bounds!=null) {
            SpuBoundsTo spuBoundsTo = new SpuBoundsTo();
            spuBoundsTo.setBuyBounds(bounds.getBuyBounds());
            spuBoundsTo.setGrowBounds(bounds.getGrowBounds());
            spuBoundsTo.setSpuId(spuInfoEntity.getId());
            R r1 = couponFeignService.saveSpuBounds(spuBoundsTo);
        }
        List<Skus> skus = spuVo.getSkus();
        if (!skus.isEmpty()) {
            for (Skus sku : skus) {
                //pms_sku_info spu对应的sku信息
                SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
                skuInfoEntity.setSpuId(spuInfoEntity.getId());
                //默认图
                List<Images> images1 = sku.getImages();
                if (!images.isEmpty()) {
                    Images images2 = images1.stream().filter(img -> {
                        return img.getDefaultImg().equals(1);
                    }).findFirst().get();
                    skuInfoEntity.setSkuDefaultImg(images2.getImgUrl());
                }
                skuInfoEntity.setSaleCount(0L);
                skuInfoEntity.setPrice(sku.getPrice());
                skuInfoEntity.setSkuSubtitle(sku.getSkuSubtitle());
                skuInfoEntity.setSkuTitle(sku.getSkuTitle());
                skuInfoEntity.setBrandId(spuInfoEntity.getBrandId());
                skuInfoEntity.setCatalogId(spuInfoEntity.getCatalogId());
                skuInfoEntity.setSkuDesc(spuInfoEntity.getSpuDescription());
                skuInfoEntity.setSkuName(sku.getSkuName());
                skuInfoDao.insert(skuInfoEntity);
                //pms_sku_images 具体的摸一个sku的图片信息
                List<Images> images2 = sku.getImages();
                if (!images2.isEmpty()) {
                    List<SkuImagesEntity> collect = images2.stream().filter(img -> {
                        return StringUtils.isNotEmpty(img.getImgUrl());
                    }).map(image -> {
                        SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                        skuImagesEntity.setSkuId(skuInfoEntity.getSkuId());
                        skuImagesEntity.setDefaultImg(image.getDefaultImg());
                        skuImagesEntity.setImgUrl(image.getImgUrl());
                        skuImagesEntity.setImgSort(0);
                        return skuImagesEntity;
                    }).collect(Collectors.toList());
                    skuImagesService.saveBatch(collect);
                }
                //pms_sku_sale_attr_value sku的销售属性
                List<Attr> attrs = sku.getAttr();
                if (!attrs.isEmpty()) {
                    List<SkuSaleAttrValueEntity> collect = attrs.stream().map(attr -> {
                        SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                        skuSaleAttrValueEntity.setAttrId(attr.getAttrId());
                        skuSaleAttrValueEntity.setAttrName(attr.getAttrName());
                        skuSaleAttrValueEntity.setAttrValue(attr.getAttrValue());
                        skuSaleAttrValueEntity.setSkuId(skuInfoEntity.getSkuId());
                        AttrEntity attrEntity = attrDao.selectById(attr.getAttrId());
                        skuSaleAttrValueEntity.setSearchType(attrEntity.getSearchType());
                        return skuSaleAttrValueEntity;
                    }).collect(Collectors.toList());
                    skuSaleAttrValueService.saveBatch(collect);
                }
                //sms_sku_ladder 打折表
                SkuLadderTo skuLadderTo = new SkuLadderTo();
                skuLadderTo.setAddOther(sku.getCountStatus());
                skuLadderTo.setFullCount(sku.getFullCount());
                skuLadderTo.setDiscount(sku.getDiscount());
                skuLadderTo.setSkuId(skuInfoEntity.getSkuId());
                R r2 = couponFeignService.saveSkuLadder(skuLadderTo);
                //sms_sku_full_reduction 满减表
                SkuFullReductionTo skuFullReductionTo = new SkuFullReductionTo();
                skuFullReductionTo.setAddOther(sku.getPriceStatus());
                skuFullReductionTo.setReducePrice(sku.getReducePrice());
                skuFullReductionTo.setFullPrice(sku.getFullPrice());
                skuFullReductionTo.setSkuId(skuInfoEntity.getSkuId());
                R r3 = couponFeignService.saveSkuFullReduction(skuFullReductionTo);
                //sms_member_price 会员价格
                List<MemberPrice> memberPrices = sku.getMemberPrice();
                if (!memberPrices.isEmpty()) {
                    List<MemberPriceTo> collect = memberPrices.stream().map(memberPrice -> {
                        MemberPriceTo memberPriceTo = new MemberPriceTo();
                        memberPriceTo.setSkuId(skuInfoEntity.getSkuId());
                        memberPriceTo.setMemberPrice(memberPrice.getPrice());
                        memberPriceTo.setMemberLevelName(memberPrice.getName());
                        memberPriceTo.setMemberLevelId(memberPrice.getId());
                        return memberPriceTo;
                    }).collect(Collectors.toList());
                    R r4 = couponFeignService.saveMemberPrice(collect);
                }

            }
        }


        return R.ok();
    }

}